<template>
  <div class="app-container">
    <div class="form-container" ref="formRef" v-if="!stuInfo.isSignUp">
      <div id="printBox">
        <header class="form-header">
          <h2>2025年上海市崇明区城桥中学招收艺体特长生报名信息表</h2>
          <div class="school-info">
            <span class="underline">{{ formData.district }}</span> 区 <span class="underline">{{ formData.school }}</span> 学校
          </div>
          <p style="text-align: left">本人承诺，以上填报信息均属实。</p>
        </header>

        <div class="form-body">
          <table class="registration-table">
            <tr>
              <td width="100">学生姓名</td>
              <td width="150">
                {{ stuInfo.actualName }}
              </td>
              <td width="130">中考报名号</td>
              <td width="200">
                {{ stuInfo.registerNum }}
              </td>
              <td rowspan="4" width="120" class="photo-cell">
                <div class="photo-container">
                  <img :src="stuInfo.picUrl" alt="照片预览" class="photo-preview" />
                </div>
              </td>
            </tr>
            <tr>
              <td>性别</td>
              <td>
                {{ stuInfo.gender == 1 ? '男' : '女' }}
              </td>
              <td>身份证号</td>
              <td>
                {{ stuInfo.card }}
              </td>
            </tr>
            <tr>
              <td>班级职务</td>
              <td>
                {{ stuInfo.position }}
              </td>
              <td>户籍地址</td>
              <td>
                {{ stuInfo.houseLocation }}
              </td>
            </tr>
            <tr>
              <td>政治面貌</td>
              <td>
                {{ stuInfo.political }}
              </td>
              <td>居住地址</td>
              <td>
                {{ stuInfo.address }}
              </td>
            </tr>
            <tr>
              <td>监护人称谓</td>
              <td>姓名</td>
              <td>联系电话</td>
              <td colspan="2">工作单位</td>
            </tr>
            <tr v-for="(item, index) in stuInfo.guardian" :key="index">
              <td>
                {{ item.guardianTitle }}
              </td>
              <td>
                {{ item.guardianName }}
              </td>
              <td>
                {{ item.phone }}
              </td>
              <td colspan="2">
                {{ item.workplace }}
              </td>
            </tr>

            <tr></tr>

            <tr>
              <td rowspan="2">本学年一模考试成绩</td>
              <td colspan="4">
                <div class="signature-line">
                  <div>
                    <span>语文：</span>
                    <span class="signature-input" style="width: 40px">
                      {{ stuInfo.score[0] || '' }}
                    </span>

                    <span style="margin-left: 100px">数学：</span>
                    <span class="signature-input" style="width: 40px">
                      {{ stuInfo.score[1] || '' }}
                    </span>
                    <span style="margin-left: 100px">英语：</span>
                    <span class="signature-input" style="width: 40px">
                      {{ stuInfo.score[2] || '' }}
                    </span>
                  </div>
                </div>
              </td>
            </tr>

            <tr>
              <!-- <td colspan="4"></td> -->
            </tr>

            <tr>
              <td rowspan="3" style="text-align: center">特长情况</td>
            </tr>
            <!-- <tr>
              <td colspan="2" style="text-align: center">体育类</td>
              <td colspan="2" style="text-align: center">艺术类</td>
            </tr> -->
            <tr>
              <td colspan="4">
                {{ stuInfo.talent[0].subcategories || '无' }}
              </td>
            </tr>

            <tr>
              <!-- <td colspan="4"></td> -->
            </tr>

            <tr>
              <td rowspan="7" style="text-align: center">特长佐证材料</td>
            </tr>
            <tr>
              <td style="text-align: center">成绩或证书获取时间</td>
              <td style="text-align: center">成绩或证书名称</td>
              <td style="text-align: center">成绩或证书名次或等级</td>
              <td style="text-align: center" colspan="3">材料附件</td>
            </tr>

            <tr v-for="(item, index) in stuInfo.talentfileEntities" :key="index">
              <td>{{ item.issuanceDate }}</td>
              <td>
                {{ item.issuanceDepartment }}
              </td>
              <td>
                {{ item.awardName }}
              </td>
              <td colspan="2" style="width: 100px; height: 100px">
                <img :src="item.talentFileUrl" alt="照片预览" style="width: 100px; height: 100px" />
              </td>
            </tr>

            <tr>
              <!-- <td colspan="4"></td> -->
            </tr>

            <tr>
              <td rowspan="5" style="text-align: center">其他获奖及荣誉</td>
            </tr>
            <tr>
              <td style="text-align: center">获奖时间</td>
              <td style="text-align: center">奖项名称</td>
              <td style="text-align: center">成绩或证书等级</td>
              <td style="text-align: center" colspan="3">材料附件</td>
            </tr>

            <tr v-for="(item, index) in stuInfo.certificateEntities" :key="index">
              <td>
                {{ item.issuanceDate }}
              </td>
              <td>
                {{ item.issuanceDepartment }}
              </td>
              <td>
                {{ item.awardName }}
              </td>
              <td colspan="2" style="width: 100px; height: 100px">
                <img :src="item.certificateUrl" alt="照片预览" style="width: 100px; height: 100px" />
              </td>
            </tr>

            <tr>
              <!-- <td colspan="4"></td> -->
            </tr>

            <tr>
              <td rowspan="2">报考承诺</td>
              <td colspan="4" class="declaration-cell">
                <div class="signature-line">
                  <div>
                    <span>学生签字：</span>
                    <span class="signature-input">
                      <!-- <input v-model="formData.studentSignature" type="text" placeholder="请签名"> -->
                    </span>

                    <span style="margin-left: 100px">监护人签字：</span>
                    <span class="signature-input">
                      <!-- <input v-model="formData.guardianSignature" type="text" placeholder="请签名"> -->
                    </span>
                  </div>
                  <!-- <div>
                  <span>监护人签字：</span>
                  <span class="signature-input">
                    
                  </span>
                </div> -->
                </div>
                <div class="date-line">
                  <span>{{ formData.signatureYear }}</span> 年 <span>{{ formData.signatureMonth }}</span> 月
                  <span>{{ formData.signatureDay }}</span> 日
                </div>
              </td>
            </tr>
            <tr>
              <!-- <td colspan="4"></td> -->
            </tr>
            <tr>
              <td rowspan="2">学校审核</td>
              <td colspan="4" class="school-review-cell">
                <p>初中学校审核意见：</p>
                <div class="school-seal">
                  学校盖章
                  <div class="date-line school-date">
                    <span>{{ formData.schoolYear }}</span> 年 <span>{{ formData.schoolMonth }}</span> 月 <span>{{ formData.schoolDay }}</span> 日
                  </div>
                </div>
              </td>
            </tr>
            <tr>
              <!-- <td colspan="4"></td> -->
            </tr>
            <tr>
              <td>备注</td>
              <td colspan="4" class="note-cell">
                <p>填报完成后，此表由学生本人及监护人签字确认，并由初中学校审核盖章，入围校测的学生于校测当日交至招生学校。</p>
              </td>
            </tr>
          </table>
        </div>
      </div>
      <div class="form-actions">
        <!-- <button class="btn btn-primary" @click="submitForm">提交</button> -->
        <button class="btn btn-secondary" v-print="print">打印</button>
        <!-- <button class="btn btn-danger" @click="resetForm">重置</button> -->
      </div>
    </div>
    <div v-else>
      <a-empty>
        <template #description>
          <span>
            请先填写报名信息
            <!-- <a href="#api">请先填写报名信息</a> -->
          </span>
        </template>
      </a-empty>
    </div>
  </div>
</template>

<script setup>
  import { ref, reactive, onMounted } from 'vue';
  import { fileApi } from '/@/api/support/file-api';
  import { loginApi } from '/@/api/system/login-api';
  // import { useUserStore } from '/@/store/modules/system/user';
  // 表单数据
  const formData = reactive({
    picUrl: '',
    district: '',
    school: '',
    actualName: '',
    registerNum: '',
    gender: '',
    card: '',
    position: '',
    houseLocation: '',
    political: '',
    address: '',
    guardianTitle: '',
    guardianName: '',
    phone: '',
    workplace: '',
    guardianTitle2: '',
    guardianName2: '',
    phone2: '',
    workplace2: '',
    remark: '',
    signatureYear: new Date().getFullYear(),
    signatureMonth: new Date().getMonth() + 1,
    signatureDay: new Date().getDate(),
    schoolYear: new Date().getFullYear(),
    schoolMonth: new Date().getMonth() + 1,
    schoolDay: new Date().getDate(),
  });

  //这里是打印的配置项
  const print = ref({
    id: 'printBox', //这里的id就是上面我们的打印区域id，实现指哪打哪
    popTitle: '配置页眉标题', // 打印配置页上方的标题
    extraHead: '', // 最上方的头部文字，附加在head标签上的额外标签，使用逗号分割
    preview: false, // 是否启动预览模式，默认是false
    previewTitle: '预览的标题', // 打印预览的标题
    previewPrintBtnLabel: '预览结束，开始打印', // 打印预览的标题下方的按钮文本，点击可进入打印
    zIndex: 20002, // 预览窗口的z-index，默认是20002，最好比默认值更高
    previewBeforeOpenCallback() {
      console.log('正在加载预览窗口！');
    }, // 预览窗口打开之前的callback
    previewOpenCallback() {
      console.log('已经加载完预览窗口，预览打开了！');
    }, // 预览窗口打开时的callback
    beforeOpenCallback() {
      console.log('开始打印之前！');
    }, // 开始打印之前的callback
    openCallback() {
      console.log('执行打印了！');
    }, // 调用打印时的callback
    closeCallback() {
      console.log('关闭了打印工具！');
    }, // 关闭打印的callback(无法区分确认or取消)
    clickMounted() {
      console.log('点击v-print绑定的按钮了！');
    },
  });

  // 照片上传
  const photoUrl = ref('');
  const loginName = ref('');
  const employeeId = ref();
  const formRef = ref(null);

  // 查询登录信息
  async function getLoginInfo() {
    try {
      //获取登录用户信息
      const res = await loginApi.getLoginInfo();
      let data = res.data;
      employeeId.value = data.employeeId;
    } catch (e) {
      smartSentry.captureError(e);
    }
    getStuInfoway();
  }
  const stuInfo = ref({
    score: ['', '', ''],
    talent: [{ subcategories: '' }],
  });
  async function getStuInfoway() {
    let obj = {
      employeeId: employeeId.value,
    };
    let res = await loginApi.getStuInfo(obj);
    if (res.code == 0) {
      stuInfo.value = res.data;

      console.log(res.data);

      try {
        stuInfo.value.score = JSON.parse(stuInfo.value.score);
        stuInfo.value.score = stuInfo.value.score.split(',').map(Number);
        stuInfo.value.fileUrl = JSON.parse(stuInfo.value.fileUrl);
        stuInfo.value.fileUrl = stuInfo.value.fileUrl.split(',');
      } catch (error) {
        console.log(error);
      }
    }
    // 自动填充当前日期
    onMounted(() => {
      formData.district = localStorage.getItem('district');
      formData.school = localStorage.getItem('school');
      getLoginInfo();
      const today = new Date();
      formData.signatureYear = today.getFullYear();
      formData.signatureMonth = today.getMonth() + 1;
      formData.signatureDay = today.getDate();
      formData.schoolYear = today.getFullYear();
      formData.schoolMonth = today.getMonth() + 1;
      formData.schoolDay = today.getDate();
    });
  }
</script>

<style>
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  body {
    font-family: 'Microsoft YaHei', 'SimSun', sans-serif;
    background-color: #f5f5f5;
    color: #333;
    line-height: 1.5;
  }

  .app-container {
    max-width: 900px;
    margin: 20px auto;
    padding: 20px;
  }

  .form-container {
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    padding: 30px;
  }

  .form-header {
    text-align: center;
    margin-bottom: 20px;
  }

  .form-header h1 {
    font-size: 18px;
    margin-bottom: 10px;
  }

  .form-header h2 {
    font-size: 20px;
    margin-bottom: 15px;
  }

  .school-info {
    font-size: 16px;
    margin-bottom: 20px;
  }

  .underline {
    display: inline-block;
    min-width: 100px;
    border-bottom: 1px solid #000;
    text-align: center;
    padding: 0 5px;
  }

  .registration-table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 20px;
  }

  .registration-table td {
    border: 1px solid #000;
    padding: 8px;
    vertical-align: middle;
    height: 40px;
  }

  input,
  select,
  textarea {
    width: 100%;
    padding: 5px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-family: inherit;
    font-size: 14px;
  }

  textarea {
    resize: none;
    height: 120px;
  }

  .photo-cell {
    text-align: center;
    vertical-align: middle;
  }

  .photo-container {
    position: relative;
    width: 100%;
    height: 160px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px dashed #ccc;
  }

  .photo-placeholder {
    text-align: center;
    font-size: 14px;
    color: #999;
    line-height: 1.5;
  }

  .photo-preview {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
  }

  .photo-input {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
  }

  .self-intro-cell {
    position: relative;
  }

  .word-count {
    position: absolute;
    bottom: 5px;
    right: 10px;
    font-size: 12px;
    color: #999;
  }

  .declaration-cell,
  .school-review-cell,
  .note-cell {
    padding: 15px;
  }

  .signature-line {
    display: flex;
    justify-content: space-between;
    margin: 20px 0;
  }

  .signature-input {
    display: inline-block;
    width: 150px;
    border-bottom: 1px solid #000;
  }

  .date-line {
    text-align: right;
    margin-top: 10px;
  }

  .school-seal {
    text-align: right;
    margin-top: 50px;
  }

  .school-date {
    margin-top: 5px;
  }

  .form-actions {
    display: flex;
    justify-content: center;
    gap: 15px;
    margin-top: 30px;
  }

  .btn {
    padding: 10px 20px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
    transition: background-color 0.3s;
  }

  .btn-primary {
    background-color: #1890ff;
    color: #fff;
  }

  .btn-primary:hover {
    background-color: #40a9ff;
  }

  .btn-secondary {
    background-color: #52c41a;
    color: #fff;
  }

  .btn-secondary:hover {
    background-color: #73d13d;
  }

  .btn-danger {
    background-color: #ff4d4f;
    color: #fff;
  }

  .btn-danger:hover {
    background-color: #ff7875;
  }

  @media (max-width: 768px) {
    .app-container {
      padding: 10px;
    }

    .form-container {
      padding: 15px;
    }

    .signature-line {
      flex-direction: column;
      gap: 10px;
    }

    .form-actions {
      flex-direction: column;
    }

    .btn {
      width: 100%;
    }
  }
</style>
